package ltd.newbee.mall.api.admin;

import ltd.newbee.mall.dao.UserMapper;
import ltd.newbee.mall.entity.User;
import ltd.newbee.mall.service.impl.UserService;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage-api/v1/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        try {
            List<User> users = userService.getUserList(params);
            return ResultGenerator.genSuccessResult(users);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取用户列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/{userId}")
    public Result getUser(@PathVariable Long userId) {
        try {
            User user = userService.getUserById(userId);
            return ResultGenerator.genSuccessResult(user);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取用户信息失败: " + e.getMessage());
        }
    }

    @PostMapping
    public Result add(@RequestBody User user) {
        try {
            //默认密码
            user.setLoginPassword("e10adc3949ba59abbe56e057f20f883e");
            boolean success = userService.addUser(user);
            return success ? ResultGenerator.genSuccessResult("添加成功") :
                    ResultGenerator.genFailResult("添加失败");
        } catch (Exception e) {
            return ResultGenerator.genFailResult("添加用户失败: " + e.getMessage());
        }
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        try {
            boolean success = userService.updateUser(user);
            return success ? ResultGenerator.genSuccessResult("更新成功") :
                    ResultGenerator.genFailResult("更新失败");
        } catch (Exception e) {
            return ResultGenerator.genFailResult("更新用户失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{userId}")
    public Result delete(@PathVariable Long userId) {
        try {
            boolean success = userService.deleteUser(userId);
            return success ? ResultGenerator.genSuccessResult("删除成功") :
                    ResultGenerator.genFailResult("删除失败");
        } catch (Exception e) {
            return ResultGenerator.genFailResult("删除用户失败: " + e.getMessage());
        }
    }

    @PostMapping("/{userId}/roles")
    public Result updateUserRoles(@PathVariable Long userId, @RequestBody List<Long> roleIds) {
        try {
            // 验证角色数量
            if (roleIds != null && roleIds.size() > 1) {
                return ResultGenerator.genFailResult("一个用户只能分配一个角色");
            }

            // 检查用户是否已经分配过角色
            if (userService.hasUserRole(userId,roleIds.get(0))) {
                return ResultGenerator.genFailResult("该用户已分配过角色，无法再次分配");
            }

            boolean success = userService.updateUserRoles(userId, roleIds);
            return success ? ResultGenerator.genSuccessResult("权限更新成功") :
                    ResultGenerator.genFailResult("权限更新失败");
        } catch (Exception e) {
            return ResultGenerator.genFailResult("更新用户权限失败: " + e.getMessage());
        }
    }
}
